//
//  AddTheContactViewController.m
//  WE
//
//  Created by 要帅气的生活 on 15/12/2.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "AddTheContactViewController.h"
#import "Size.h"
#import "ToolUtil.h"
#import "ChooseView.h"
#import "DatePikerView.h"
#import "DateUtil.h"
#import "YcKeyBoardView.h"

@interface AddTheContactViewController ()<UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource,YcKeyBoardViewDelegate>

{
    UIImageView *genderImage;
    ChooseView *chooseView;
    ChooseView *chooseView1;
    UIDatePicker *datep;
    NSInteger k;
    NSTimer *timer;
    UIView *pikView;
    DatePikerView *datepik;
    UIView *pView;
    float height;
    UITextField *textF;
}

@property (nonatomic,strong)YcKeyBoardView *key;
@property (nonatomic,assign) CGFloat keyBoardHeight;
@property (nonatomic,assign) CGRect originalKey;
@property (nonatomic,assign) CGRect originalText;
@end

@implementation AddTheContactViewController
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
    self.view.frame = CGRectMake(0, 0, WindowWidth, WindowHeight);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"添加联系人";
    
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStyleDone target:self action:@selector(save)];
    self.navigationItem.rightBarButtonItem = right;
    datepik = [[DatePikerView alloc] initWithFrame:CGRectMake(0, WindowHeight - 0, WindowWidth, 320)];
    datepik.hidden = YES;
    datepik.cancleButton.tag = 120;
    datepik.confirmButton.tag = 121;
    datepik.datePicker.maximumDate = [NSDate date];
    datepik.backgroundColor = GRAYCOLOR;
    [datepik.cancleButton addTarget:self action:@selector(closeDatePiker:) forControlEvents:UIControlEventTouchUpInside];
    [datepik.confirmButton addTarget:self action:@selector(closeDatePiker:) forControlEvents:UIControlEventTouchUpInside];
    
    pView = [[UIView alloc] initWithFrame:CGRectMake(0, WindowHeight - 200, WindowWidth, 200)];
    pView.backgroundColor = GRAYCOLOR;
    pView.hidden = YES;
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    loginButton.frame = CGRectMake(310*Width, 10, 60, 30);
    [loginButton setTitle:@"确定" forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(pressPview) forControlEvents:UIControlEventTouchUpInside];
    loginButton.backgroundColor = [UIColor blueColor];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [pView addSubview:loginButton];
    
    UIPickerView *pickerV = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 50, WindowWidth, 150)];
    
    pickerV.delegate = self;
    pickerV.dataSource = self;
    [pView addSubview:pickerV];
    [self name];
    [self.view addSubview:datepik];
    [self.view addSubview:pView];
}

- (void)pressPview{
    pView.hidden = YES;
//    [self recover];
}
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    return @"杭州";
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return 10;
}
-(void)save{
    NSLog(@"保存");
    
}

- (void)closeDatePiker:(UIButton*)button{
    if (button.tag == 121) {
       // NSDateFormatter
        
      NSInteger year = [[DateUtil getString:[NSDate date] :@"yyyy"] integerValue];
      NSInteger j = [[DateUtil getString:datepik.datePicker.date :@"yyyy"] integerValue];
      NSInteger mouth = [[DateUtil getString:datepik.datePicker.date :@"MM"] integerValue];
        NSInteger mouth1 = [[DateUtil getString:[NSDate date] :@"MM"] integerValue];
        if (mouth1 >= mouth) {
            year = year - j + 1;
            
        }else {
            year = year - j-1;
        }
        chooseView1.label.text = [NSString stringWithFormat:@"%ld 岁",year];
        chooseView1.label.textColor = [UIColor blackColor];
    }
    [self recover];
    pView.hidden = YES;
}


- (void)recover{
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.3];//动画时间长度，单位秒，浮点数
    [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
    //    datepik.hidden = YES;
    datepik.frame = CGRectMake(0, WindowHeight - 0, WindowWidth, 320);
    
    [UIView setAnimationDelegate:self];
    // 动画完毕后调用animationFinished
    [UIView setAnimationDidStopSelector:@selector(animationFinished)];
    [UIView commitAnimations];
}

-(void)name{
    NSArray *arr = @[@"名字",@"身份证",@"性别",@"年龄",@"地址",@"手机号"];
    NSArray *array =@[@"请输入真实的名字",@"请输入真实的身份证号"];
    for (int i = 0; i<6 ; i++) {
        UIView *cell = [[UIView alloc] initWithFrame:CGRectMake(0, 64+i*50, WindowWidth, 50)];
        cell.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:cell];
        UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, WindowWidth/4, 30)];
        name.textColor = [UIColor blackColor];
        [cell addSubview:name];
        //字体大小
        name.font = [UIFont systemFontOfSize:14];
        name.text = arr[i];
        name.textAlignment = 0;
        name.alpha = 0.6;
        [ToolUtil createLine:49 superView:cell lift:10 width:WindowWidth-10 hight:1 color:GRAYCOLOR];
        if (i == 0 || i == 1) {
            
            UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(WindowWidth/4, 10, WindowWidth/2, 30)];
            [cell addSubview:field];
            field.tag = i+430;
            field.font = FONT(14);
            field.delegate = self;
            field.placeholder = array[i];
            [cell addSubview:field];
        }
            if (i == 2) {
                chooseView = [[ChooseView alloc] initWithFrame:CGRectMake(WindowWidth/4, 0, WindowWidth/4*3, 49)];
                chooseView.button.tag = 200+i;
                [cell addSubview:chooseView];
                genderImage = [[UIImageView alloc] initWithFrame:CGRectMake(50, 15, 20, 20)];
                genderImage.layer.cornerRadius = 10;
                genderImage.layer.masksToBounds = YES;
                genderImage.backgroundColor = [UIColor whiteColor];
                [chooseView addSubview:genderImage];
                [chooseView.button addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
        }
        if (i == 3) {
            chooseView1 = [[ChooseView alloc] initWithFrame:CGRectMake(WindowWidth/4, 0, WindowWidth/4*3, 49)];
            chooseView1.button.tag = 200+i;
            [cell addSubview:chooseView1];
            [chooseView1.button addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        if (i == 4) {
            UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
            loginButton.frame = CGRectMake(WindowWidth/4,0,WindowWidth/4*3, 49);
            loginButton.tag = 200+i;
            //loginButton.alpha = 0.9;
            [loginButton addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
            loginButton.backgroundColor = [UIColor whiteColor];
            //[loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [cell addSubview:loginButton];
            float arrow_h = 0.36 * 50 ,arrow_w = arrow_h * 0.57 ,arrow_y = (50 - arrow_h)/2  , arrow_x = WindowWidth/4*3 - 30;
            UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(arrow_x, arrow_y, arrow_w , arrow_h)];
            imageV.image = [UIImage imageNamed:@"ico_arrow"];
            [loginButton addSubview:imageV];
            [cell addSubview:loginButton];
           
                CGFloat wid = [ToolUtil autoWidth:@"浙江 杭州" width:0 height:30 num:14];
                
                UILabel *cityL = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, wid, 30)];
                cityL.font = FONT(14);
                cityL.text = @"浙江 杭州";
                cityL.textColor = [UIColor blackColor];
                [loginButton addSubview:cityL];
                UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(wid +10, 10, 100, 30)];
                label.text = @"(自动定位)";
                label.font = FONT(13);
                label.textColor = GRAYCOLOR;
                [loginButton addSubview:label];
          }
        if (i == 5) {
            textF = [[UITextField alloc] initWithFrame:CGRectMake(WindowWidth/4, 10, WindowWidth/4*3, 30)];
            textF.placeholder = @"用于接收预约短信,请谨慎填写";
            textF.delegate = self;
            textF.font = FONT(14);
            [cell addSubview:textF];
            CGFloat arrow_h = 0.36 * 50 ,arrow_w = arrow_h * 0.57 ,arrow_y = (30 - arrow_h)/2  , arrow_x = WindowWidth/4*3 - 30;
            UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(arrow_x, arrow_y, arrow_w , arrow_h)];
            imageV.image = [UIImage imageNamed:@"ico_arrow"];
            [textF addSubview:imageV];
        }
     
    }
    
    
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
//    
//    if(self.key==nil){
//        self.key=[[YcKeyBoardView alloc]initWithFrame:CGRectMake(0, WindowHeight-44, WindowWidth, 44)];
//    }
//    self.key.delegate=self;
//    [self.key.textView becomeFirstResponder];
//    self.key.textView.returnKeyType=UIReturnKeySend;
//    [self.view addSubview:self.key];
    if (textField == textF) {
        self.view.frame = CGRectMake(0, -216, WindowWidth, WindowHeight);
    }
    [self recover];
    
    pView.hidden = YES;
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
     self.view.frame = CGRectMake(0, 0, WindowWidth, WindowHeight);
    [textField resignFirstResponder];
    
    
    return YES;
}

-(void)keyboardShow:(NSNotification *)note
{
    CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat deltaY=keyBoardRect.size.height;
    self.keyBoardHeight=deltaY;
    [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
        
        self.key.transform=CGAffineTransformMakeTranslation(0, -deltaY);
    }];
}
-(void)keyboardHide:(NSNotification *)note
{
    [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
        
        self.key.transform=CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        
        self.key.textView.text=@"";
        [self.key removeFromSuperview];
    }];
    
}
-(void)keyBoardViewHide:(YcKeyBoardView *)keyBoardView textView:(UITextView *)contentView
{
    
    
    [contentView resignFirstResponder];
    //接口请求
    
}


- (void)pressButton:(UIButton*)btu{
    pView.hidden = YES;
    if (btu.tag == 202) {
        [self recover];
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"性别" message:@"" preferredStyle:(UIAlertControllerStyleActionSheet)];
        UIAlertAction * CameraAction = [UIAlertAction actionWithTitle:@"男" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            genderImage.image = [UIImage imageNamed:@"boy"];
            chooseView.label.text = @"男";
            chooseView.label.textColor = [UIColor blackColor];
            
        }];
        UIAlertAction * AlbumAction = [UIAlertAction actionWithTitle:@"女" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            genderImage.image = [UIImage imageNamed:@"girl"];
            chooseView.label.text = @"女";
            chooseView.label.textColor = [UIColor blackColor];
        }];
        UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"h");
            genderImage.image = [UIImage imageNamed:@""];
            chooseView.label.text = @"必填";
            chooseView.label.textColor = GRAYCOLOR;
        }];
        [alertController addAction:CameraAction];
        [alertController addAction:AlbumAction];
        [alertController addAction:cancelAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }else if (btu.tag == 203){
        datepik.hidden = NO;
        [self.view endEditing:YES];
        self.view.frame = CGRectMake(0, 0, WindowWidth, WindowHeight);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [UIView beginAnimations:nil context:context];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDuration:0.3];//动画时间长度，单位秒，浮点数
        datepik.frame = CGRectMake(0, WindowHeight - 320, WindowWidth,320);
        [UIView setAnimationDelegate:self];
        // 动画完毕后调用animationFinished
        [UIView setAnimationDidStopSelector:@selector(animationFinished)];
        [UIView commitAnimations];
    }else if(btu.tag == 204){
        [self.view endEditing:YES];
        pView.hidden= NO;
        [self recover];
        
    }
}
- (void)animationFinished{
    
}

@end
